﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class SelectCust : BasePage
    {
        protected int PolicyId =0;
        ICustomerBLL custBLL = (ICustomerBLL)IocContainer.Instance.Resolve<ICustomerBLL>();



        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["policyId"], out PolicyId);
            //if (!Page.IsPostBack)
            //{
            //    BindData();
            //}
            BindData();
        }

        private void BindData()
        {
            try
            {
                StringBuilder strWhere = new StringBuilder();
                if (!string.IsNullOrEmpty(this.txtCustomerCode.Text))
                {
                    strWhere.Append(" and FNumber like '%" + this.txtCustomerCode.Text + "%'");
                }
                if (!string.IsNullOrEmpty(this.txtCustomerName.Text))
                {
                    strWhere.Append(" and FName like '%" + this.txtCustomerName.Text + "%'");
                }
                if (!string.IsNullOrEmpty(PolicyId.ToString()))
                {
                    strWhere.Append(" and not exists(select * from T_Sales_PriceCust as tsp where tsp.PolicyId =" + PolicyId + " and tsp.CustId = T_BasicData_Customer.FItemId) ");
                }

                this.DataGrid1.Visible = true;
                 DataTable dt = custBLL.Query(strWhere.ToString());
                 if (dt != null && dt.Rows.Count > 0)
                 {
                     this.DataGrid1.DataSource = dt;
                     this.DataGrid1.DataBind();
                 }
                 else
                 {
                     this.DataGrid1.Visible = false;
                 }

            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
        }

     


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strids = "";
            //Boolean result = false;
            try
            {
                strids = HdnSelectedValues.Value;
                if (strids != "")
                {
                    strids = strids.Substring(1);
                    string[] arr = strids.Split(',');
                    for (int m = 0; m < arr.Length; m++)
                    {
                        custBLL.AddPriceCust(PolicyId, int.Parse(arr[m]));
                    }

                }
                ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");

            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
        }


        protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            //重新显示所选择的项目 
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (HdnSelectedValues.Value.IndexOf(((Literal)e.Item.Cells[1].FindControl("TitleShow")).Text) >= 0)
                {
                    HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("ChkSelect"));
                    ChkSelected.Checked = true;
                }
            }

        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindData();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }









    }
}
